package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * projectName: wokepase1
 *
 * @author: 强力
 * time: 2021/9/11 14:41
 * description:
 */
@Service
public class AdminServiceImpl implements AdminService{

    @Autowired
    private DtsAdminMapper dtsAdminMapper;

    @Override
    public List<DtsAdmin> findAdmin(String username) {
        QueryWrapper<DtsAdmin> admin=new QueryWrapper<>();
        admin.eq("username",username);

        List<DtsAdmin> admins = dtsAdminMapper.selectList(admin);
        return admins;
    }

    @Override
    public IPage<DtsAdmin> findByPage(Integer page, Integer limit, String username, String sort, String order) {
        //分页参数
        Page<DtsAdmin> adminPage=new Page<>(page,limit);

        //查询条件
        QueryWrapper<DtsAdmin> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)){
        queryWrapper.like("username",username);
        }

        if ("asc".equalsIgnoreCase(order)) {
            queryWrapper.orderByAsc(sort);
        }else {
            queryWrapper.orderByDesc(sort);
        }

        IPage<DtsAdmin> pagelist =  dtsAdminMapper.selectPage(adminPage, queryWrapper);
        return pagelist;
    }

    @Override
    public DtsAdmin insert(DtsAdmin admin) {

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        admin.setPassword(encoder.encode(admin.getPassword()));
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        //未删除状态
        admin.setDeleted(false);
        dtsAdminMapper.insert(admin);
        return admin;
    }

    @Override
    public int delete(DtsAdmin admin) {
        QueryWrapper<DtsAdmin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id", admin.getId());
        int count = dtsAdminMapper.delete(queryWrapper);
        return count;
    }

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        QueryWrapper<DtsAdmin>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",admin.getId());
        BCryptPasswordEncoder encoder =new BCryptPasswordEncoder();
        admin.setPassword(encoder.encode(admin.getPassword()));
        dtsAdminMapper.update(admin,queryWrapper);
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        return admin;
    }

    @Override
    public List<DtsAdmin> findAdminlist() {
        List<DtsAdmin> admins = dtsAdminMapper.selectList(null);
        return admins;
    }


}
